all files / pagination/ pagination.vue

18.46% Statements 12/65
15.38% Branches 4/26
7.69% Functions 1/13
8.62% Lines 5/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                                          1× 1× 1× 1×                                                                                                                                                                                                                                                                                                                                                
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
 
import pageItem from './pageItem'
import selectField from '../selectField'
import textField from '../textField'
import menuItem from '../menu/menuItem'
export default{
  name: 'mu-pagination',
  props: {
    total: {
      type: Number,
      default: 1
    },
    current: {
      type: Number,
      default: 1
    },
    defaultPageSize: {
      type: Number,
      default: 10
    },
    pageSize: {
      type: Number
    },
    showSizeChanger: {
      type: Boolean,
      default: false
    },
    pageSizeOption: {
      type: Array,
      default: () => ['10', '20', '30', '40']
    }
    // showQuickJumper: {
    //   type: Boolean,
    //   default: false
    // }
  },
  data () {
    return {
      leftDisabled: false,
      rightDisabled: false,
      actualCurrent: this.current,
      actualPageSize: this.defaultPageSize,
      totalPageCount: 0,
      pageList: [],
      quickJumpPage: ''
    }
  },
  mounted () {
    this.iconIsDisabled(this.actualCurrent)
 
    // 优先使用pageSizeOption,如果props配置了默认值,那么该props无论在父组件
    // 中是否配置该值都不会为undefined,所以需要使用showSizeChanger来做这个判断
    // 才对
    if (this.showSizeChanger) {
      this.actualPageSize = this.pageSizeOption[0]
    } else if (this.pageSize) {
      this.actualPageSize = this.pageSize
    }
 
    this.totalPageCount = Math.ceil(this.total / this.actualPageSize)
    this.pageList = this.calcPageList(this.actualCurrent)
  },
  computed: {
    totalPageCount: function () {
      return Math.ceil(this.total / this.actualPageSize)
    }
  },
  methods: {
    handleClick (res) {
      if (typeof res === 'number') {
        this.actualCurrent = res
      } else {
        switch (res) {
          case 'singleBack':
            this.actualCurrent = Math.max(1, this.actualCurrent - 1)
            break
          case 'backs':
            this.actualCurrent = Math.max(1, this.actualCurrent - 5)
            break
          case 'forwards':
            this.actualCurrent = Math.min(this.totalPageCount, this.actualCurrent + 5)
            break
          case 'singleForward':
            this.actualCurrent = Math.min(this.totalPageCount, this.actualCurrent + 1)
            break
        }
      }
    },
    iconIsDisabled (current) {
      this.leftDisabled = current === 1
      this.rightDisabled = current === this.totalPageCount
    },
 
    calcPageList (current) {
      let pageList = []
 
      if (this.totalPageCount > 5) {
        let left = Math.max(2, current - 2)
        let right = Math.min(current + 2, this.totalPageCount - 1)
        if (current - 1 < 2) {
          right = 4
        }
 
        if (this.totalPageCount - current < 2) {
          left = this.totalPageCount - 3
        }
 
        for (let i = left; i <= right; i++) {
          pageList.push(i)
        }
      } else {
        for (let i = 2; i < this.totalPageCount; i++) {
          pageList.push(i)
        }
      }
 
      return pageList
    },
    pageSizeAndTotalChange (current) {
      this.iconIsDisabled(current)
      this.pageList = this.calcPageList(current)
    }
    // quickJump () {
    //   if (this.quickJumpPage) {
    //     this.actualCurrent = Math.min(this.quickJumpPage, this.totalPageCount)
    //   }
    // }
  },
  components: {
    'page-item': pageItem,
    'select-field': selectField,
    'text-field': textField,
    'menu-item': menuItem
  },
  watch: {
    actualCurrent: function (val) {
      if (val === 0) {
        return
      }
      this.pageSizeAndTotalChange(val)
      this.$emit('pageChange', val)
    },
    actualPageSize: function (val, oldVal) {
      // 如果页面条数改变的时候,对应的当前页也是需要重新计算的,
      // 计算规则是根据当前页的起始索引来计算该索引位于新的pageSize
      // 中的页码
      let itemIndex = oldVal * (this.actualCurrent - 1)
      let oldCurrent = this.actualCurrent
      this.actualCurrent = Math.floor(itemIndex / val) + 1
      // 页码条数改变的时候当前页不一定改变,但是我们必须重新计算一些依赖的参数
      if (oldCurrent === this.actualCurrent) {
        this.pageSizeAndTotalChange(oldCurrent)
      }
      this.$emit('pageSizeChange', val)
    },
    total: function (val) {
      // 如果条目总数改变的时候当前页也需要重新计算
      let oldCurrent = this.actualCurrent
      this.actualCurrent = Math.min(this.totalPageCount, this.actualCurrent)
      // 总条数改变的时候当前页不一定改变,但是我们必须重新计算一些依赖的参数,
      // 比如total从10变为11(pageSize=10),那么current没变,不过右前进的按钮应该由
      // disable变为enable的
      if (oldCurrent === this.actualCurrent) {
        this.pageSizeAndTotalChange(oldCurrent)
      }
    },
    current (val) {
      this.actualCurrent = val
    }
  }
}